package com.im.common;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @Created leijun
 * @Date 2021/9/28 7:29 上午
 */
public class Message {
    /**
     * 请求头长度
     */
    private Integer headerLength;
    /**
     * 客户端sdk版本号
     */
    private Integer appSdkVersion;
    /**
     * 消息类型：request、response
     */
    private Integer messageType;
    /**
     * 请求类型
     */
    private Integer requestType;
    /**
     * 消息序号
     */
    private Long sequence;

    private Integer bodyLength;

    private byte[] body;

    private ByteBuf buf;

    public Message(ByteBuf byteBuf) {
        this.headerLength = byteBuf.readInt();
        this.appSdkVersion = byteBuf.readInt();
        this.messageType = byteBuf.readInt();
        this.requestType = byteBuf.readInt();
        this.sequence = byteBuf.readLong();
        this.bodyLength = byteBuf.readInt();
        byte[] body = new byte[bodyLength];
        byteBuf.readBytes(body);
        this.body = body;
        this.buf = byteBuf;
    }

    public Message(int appSdkVersion, int messageType, int requestType, long sequence, byte[] requestBody) {
        this.headerLength = ImConstant.REQUEST_HEADER_LENGTH;
        this.appSdkVersion = appSdkVersion;
        this.messageType = messageType;
        this.requestType = requestType;
        this.sequence = sequence;
        this.bodyLength = requestBody.length;
        this.body = requestBody;

        // 封装认证请求（请求头+请求体+分隔符）
        this.buf = Unpooled.buffer(this.headerLength + this.bodyLength + ImConstant.DELIMITER.length);
        this.buf.writeInt(this.headerLength);
        this.buf.writeInt(this.appSdkVersion);
        this.buf.writeInt(this.messageType);
        this.buf.writeInt(this.requestType);
        this.buf.writeLong(this.sequence);
        this.buf.writeInt(this.bodyLength);
        this.buf.writeBytes(this.body);
        this.buf.writeBytes(ImConstant.DELIMITER);
    }

    public Request toRequest() {
        return new Request(this.appSdkVersion, this.requestType, this.sequence, this.body);
    }

    public Response toResponse() {
        return new Response(this.appSdkVersion, this.requestType, this.sequence, this.body);
    }

    public Integer getHeaderLength() {
        return headerLength;
    }

    public void setHeaderLength(Integer headerLength) {
        this.headerLength = headerLength;
    }

    public Integer getAppSdkVersion() {
        return appSdkVersion;
    }

    public void setAppSdkVersion(Integer appSdkVersion) {
        this.appSdkVersion = appSdkVersion;
    }

    public Integer getMessageType() {
        return messageType;
    }

    public void setMessageType(Integer messageType) {
        this.messageType = messageType;
    }

    public Integer getRequestType() {
        return requestType;
    }

    public void setRequestType(Integer requestType) {
        this.requestType = requestType;
    }

    public Long getSequence() {
        return sequence;
    }

    public void setSequence(Long sequence) {
        this.sequence = sequence;
    }

    public Integer getBodyLength() {
        return bodyLength;
    }

    public void setBodyLength(Integer bodyLength) {
        this.bodyLength = bodyLength;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public ByteBuf getBuf() {
        return buf;
    }

    public void setBuf(ByteBuf buf) {
        this.buf = buf;
    }
}
